﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;

namespace RandomShapeGenerator.src
{
    public class DrawingController
    {
        private Shape pallette = new Shape();

        private IShapeRenderer currentRenderer = new RectangleRenderer();

        private List<KeyedRenderer> KeyedRendererList = new List<KeyedRenderer>();

        private class KeyedRenderer
        {
            public KeyCode Key;
            public IShapeRenderer Renderer;

            public KeyedRenderer(KeyCode k, IShapeRenderer r)
            {
                Key = k;
                Renderer = r;
            }
        }

        public void AddRenderer(KeyCode k, IShapeRenderer r)
        {
           KeyedRendererList.Add(new KeyedRenderer(k,r));
        }
        private Drawing controlling;

        public Drawing Controlling
        {
            get { return controlling; }
            set { controlling = value; }
        }

        public DrawingController()
            : this(new Drawing())
        {
            AddRenderer(KeyCode.vk_r, new RectangleRenderer());
            AddRenderer(KeyCode.vk_l, new LineRenderer());
            AddRenderer(KeyCode.vk_e, new EllipseRenderer());
            pallette.X = 0;
            pallette.Y = 0;
            pallette.Width = 30;
            pallette.Height = 30;
        }


        public DrawingController(Drawing drawing)
        {
            Controlling = drawing;
        }

        private const int DEFAULT_WIDTH = 30;
        private const int DEFAULT_HEIGHT = 30;
        private Color currentColor = Color.Green;

 //methods starts
        //drawing controller
        public void Draw()
        {
            controlling.Draw();
            
        }

        public void HandleInput()
        {
            //selecting shape
            foreach (KeyedRenderer kr in KeyedRendererList)
            {
                if(Input.KeyTyped(kr.Key)) currentRenderer = kr.Renderer;
            }

           //leftclick to add
            if (Input.MouseClicked(MouseButton.LeftButton) && controlling.SelectedShape ==null)
                AddNewShape(Input.MousePosition().x, Input.MousePosition().y);
            //rightclick to select
            if (Input.MouseClicked(MouseButton.RightButton))
                SelectShape(Input.MousePosition().x, Input.MousePosition().y);
            //shift + leftclick to resize
            if (Input.MouseDown(MouseButton.LeftButton) && (Input.KeyDown(KeyCode.vk_RSHIFT) || Input.KeyDown(KeyCode.vk_LSHIFT)) && controlling.SelectedShape != null)
                ResizeShape(Input.MousePosition().x, Input.MousePosition().y);
            //ctrl+ leftclick to move shape
            if (Input.MouseDown(MouseButton.LeftButton) && (Input.KeyDown(KeyCode.vk_RCTRL) || Input.KeyDown(KeyCode.vk_LCTRL)) && controlling.SelectedShape != null)
                MoveShape(Input.MousePosition().x, Input.MousePosition().y);
            //delete to remove.
            if (Input.KeyTyped(KeyCode.vk_DELETE) && controlling.SelectedShape != null)
                controlling.RemoveShape(controlling.SelectedShape);

            //colour
            if (Input.KeyDown(KeyCode.vk_h))
                ChangeHue();
 

        }

        public void ChangeHue()
        {
            float hue, sat, bri;
            Shape s = controlling.SelectedShape;
            Core.HSBValuesOf(currentColor, out hue, out sat, out bri);
                hue += 0.005f;
                if (controlling.SelectedShape != null)
                {
                    s.Color = Core.HSBColor(hue, sat, bri);
                    currentColor = Core.HSBColor(hue, sat, bri);
                }
                else
                    currentColor = Core.HSBColor(hue, sat, bri);
        }

        public void ResizeShape(float MousePosX, float MousePosY)
        {
            int dw, dh;
            Shape s = controlling.SelectedShape;
            dw = (int) (MousePosX - (s.X + s.Width));
            dh = (int) (MousePosY - (s.Y + s.Height));
            
            if (s != null)
            {
                s.Width += dw;
                s.Height += dh;
            }
        }

        public void MoveShape(float MousePosX, float MousePosY)
        {
            Shape s = controlling.SelectedShape;

            if (s != null)
            {
                s.X = MousePosX;
                s.Y = MousePosY;
            }
        }
        public void SelectShape(float MousePosX, float MousePosY)
        {
            controlling.SelectShapeAtPoint(MousePosX, MousePosY);
        }

        public void AddNewShape(float MousePosX, float MousePosY)
        {
            Shape s = new Shape();

            s.X = MousePosX - DEFAULT_WIDTH / 2;
            s.Y = MousePosY - DEFAULT_HEIGHT / 2;
            s.Color = currentColor;
            s.Width = DEFAULT_WIDTH;
            s.Height = DEFAULT_HEIGHT;

            s.Renderer = currentRenderer;
            controlling.AddShape(s);
        }

        


        public void RefreshPallette()
        {

            pallette.Color = currentColor;


            controlling.AddShape(pallette);


        }
 

    }

}
